import cv2
image=cv2.imread('hat.png',cv2.IMREAD_GRAYSCALE)
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))


eroded_img1=cv2.erode(image,kernel)
cv2.imshow("Eroded(3*3)",eroded_img1)

kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
dilated_img1=cv2.dilate(image,kernel,iterations=1)
cv2.imshow("1-dilate(3,3)",dilated_img1)
dilated_img2=cv2.dilate(image,kernel,iterations=2)
cv2.imshow("2-dilate(3,3)",dilated_img2)

gradient=cv2.morphologyEx(image,cv2.MORPH_GRADIENT,kernel)
cv2.imshow("Input",image)
cv2.imshow("Gradient",gradient)

image1=cv2.dilate(image,kernel)
image2=cv2.erode(image1,kernel)
cv2.imshow("Output",image2)
image3=cv2.erode(image2,kernel)
image4=cv2.dilate(image3,kernel)
cv2.imshow("Output",image4)

tophat=cv2.morphologyEx(image,cv2.MORPH_TOPHAT,kernel)
blackhat=cv2.morphologyEx(image,cv2.MORPH_BLACKHAT,kernel)
cv2.imshow("Input",image)
cv2.imshow("TopHat",tophat)
cv2.imshow("BlackHat",blackhat)








cv2.waitKey()
cv2.destroyAllWindows()